//
//  FlickrClient.h
//  iFlick1.4
//
//  Created by vhly on 10-9-11.
//  Copyright 2010 xhzhang. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "UserAccount.h"
#import "PhotoItem.h"
#import "ItemStore.h"

#define FLICKR_REST @"http://api.flickr.com/services/rest/"
#define FLICKR_AUTH @"http://m.flickr.com/services/auth/"


@interface FlickrClient : NSObject {
	
	/**
	 * Flickr API key
	 */
	NSString *apiKey;
	
	/**
	 * Flickr API Secret
	 */
	NSString *apiSecret;
	
	NSInteger networkRef;
	
	UserAccount *userAccount;
	
	NSMutableArray *images;
}

@property (nonatomic, retain) UserAccount *userAccount;

#pragma mark -
#pragma mark Constructor

+ (FlickrClient *)getInstance;

- (id)initWithAPIKey:(NSString *)key andSecret:(NSString *)sec;

#pragma mark -
#pragma mark initRESTCallInfo

- (NSMutableDictionary *)getInitParam;

- (NSString *)genAPISign:(NSDictionary *)params;

/**
 * 生成按照名称排列的key 数组
 * @param params NSDictionary 参数
 * @return NSArray 排列之后的名称
 */
- (NSArray *)getSortKeys:(NSDictionary *)params;

/**
 * 获取请求地址，主要是根据API EndPoint 和 Params参数来生成请求地址
 * @param endPoint NSString 请求入口
 * @param method NSString 请求方法
 * @param params NSDictionary 参数列表
 * @return NSString 请求地址
 */
- (NSString *)getRESTRequest:(NSString *)endPoint method:(NSString *)method withParams:(NSDictionary *)params;


#pragma mark -
#pragma mark Flickr Client Authorization
- (NSString *)getFrob;

- (NSString *)getAuthorizeURL:(NSString *)frob;

- (void)getToken:(NSString *)frob;

/**
 * 是否已经验证
 * @return BOOL
 */
- (BOOL)isAuthorized;

/**
 * 注销
 */
- (void)logout;

#pragma mark -
#pragma mark MakeAPICall

- (NSString *)invokeRESTCall:(NSString *)call;

- (void)incNetworkRef;

- (void)decNetworkRef;

#pragma mark -
#pragma mark SaveInfo

- (void)saveAuthInfo;

- (BOOL)loadAuthInfo;

#pragma mark -
#pragma mark ImageUpload

/**
 * 创建请求，采用多成分形式组装内容
 * @param params NSDictionary 参数
 * @return NSData 采用 multipart/form-data形式的数据 
 */
- (NSData *)makeUploadRequest:(NSDictionary *)params;

#pragma mark -
#pragma mark ImageSupport
/**
 * 添加图像
 * @param libraryInfo 从UIImagePickerController中获取的图像信息
 */
- (void)addImage:(NSDictionary *)libraryInfo;
/**
 * 返回当前图像个数
 * @return NSInteger
 */
- (NSInteger)imagesSize;

- (ItemStore *)getItemStore:(NSInteger)index;

@end
